/*
 * Copyright (C), 2014-2015, 联创车盟汽车服务有限公司
 * FileName: CommentController.java
 * Author:   Freeman
 * Date:     2015年2月3日 下午12:41:11
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>    <time>    <version>    <desc>
 * 修改人姓名        修改时间        版本号        描述
 */
package com.linkage.controller.commodity;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.linkage.interceptor.NeedLogin;
import com.linkage.pojo.background.UserRole;
import com.linkage.pojo.commodity.Book;
import com.linkage.pojo.commodity.Category;
import com.linkage.pojo.commodity.Comment;
import com.linkage.pojo.consumer.User;
import com.linkage.service.background.UserManageService;
import com.linkage.service.commodity.BookService;
import com.linkage.service.commodity.CategoryService;
import com.linkage.service.commodity.CommentService;
import com.linkage.service.commodity.LoginService;
import com.linkage.service.consumer.UserService;

/**
 * 评论controller层<br> 
 * 〈功能详细描述〉
 * @author Freeman
 *
 */
@Controller
@RequestMapping("/")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private LoginService loginService;
    @Autowired
    private UserManageService userManageService;
    @Autowired
    private UserService userService;
    @Autowired
    private BookService bookService;
    @Autowired
    private CategoryService categoryService;
    
    @NeedLogin
    @RequestMapping(value="/saveComment",method=RequestMethod.POST)
    public String saveComment(Comment comment,Model model){
        //FIXME 这里的代码是直接设置属性值
        comment.setFromAuthor(1);
        comment.setIsDelete(false);
        comment.setCreateTime(new Date());
        this.commentService.addComment(comment);
        model.addAttribute("bookId", comment.getBookId());
        return "commodity/success";
    }
    
    @RequestMapping(value="/listComment/{bookId}",method=RequestMethod.GET)
    public String listComment(@PathVariable("bookId")int bookId,Model model){
        List<Comment> list=this.commentService.listComments(bookId);
        model.addAttribute("comments", list);
        List<Category> list3=this.categoryService.listAllCategory();
        model.addAttribute("category", list3);
        return "commodity/book";
    }
    @NeedLogin
    @RequestMapping(value="/addComment/{bookId}",method=RequestMethod.GET)
    public String addComment(@PathVariable("bookId")int bookId,Model model){
        Comment comment=new Comment(bookId);
        model.addAttribute("comment", comment);
        List<Category> list3=this.categoryService.listAllCategory();
        model.addAttribute("category", list3);
        return "commodity/comment";
    }
    
    //这个controller先放在这儿，以后还是需要在用户层定义的
    @RequestMapping(value="/login",method=RequestMethod.POST)
    public String login(@RequestParam("loginName")String loginName,@RequestParam("password")String password,HttpServletRequest request, Model model){
        User user=this.loginService.login(password, loginName);
        if(user!=null){
//            UserRole userRole=this.userManageService.findUserRoleById(user.getUserId());
            request.getSession().setAttribute("LOGIN_USER_KEY_IN_SESSION", user);
//            model.addAttribute("userRole", userRole);
//            model.addAttribute("message", user.getUserName()+"欢迎你!");
//            request.getSession().setAttribute("message",user.getUserName()+"欢迎你!");
            List<Book> list2=this.bookService.listAllAvailableBook();
            model.addAttribute("books", list2);
            List<Category> list3=this.categoryService.listAllCategory();
            model.addAttribute("category", list3);
            return "redirect:/index";
        }
        return "commodity/loginForm";
    }
    @RequestMapping(value="/gotoLogin",method=RequestMethod.GET)
    public String gotoLogin(Model model){
        List<Category> list3=this.categoryService.listAllCategory();
        model.addAttribute("category", list3);
        return "commodity/loginForm";
    }
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    @NeedLogin
    public String logout(HttpServletRequest request,Model model) {
        this.loginService.logout(request);
        List<Book> list2=this.bookService.listAllAvailableBook();
        model.addAttribute("books", list2);
        List<Category> list3=this.categoryService.listAllCategory();
        model.addAttribute("category", list3);
        return "commodity/listBook";
    }
    
    @RequestMapping(value="/check/{loginName}",method=RequestMethod.GET,produces="application/json")
    @ResponseBody
    //TODO
    public Map<String,Boolean> check(@PathVariable("loginName")String loginName){
        Map<String,Boolean> map=new HashMap<>();
        User user=this.userService.getOneUserByLoginName(loginName);
        if(user!=null){
            map.put("result", true);
        }else{
            map.put("result", false);
        }
        return map;
    }
}
